const url = require("url");
const express = require("express");
const router = express();
const bodyParser = require('body-parser');
router.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }));
router.use(bodyParser.json({ limit: '50mb' }));
// router.use(bodyParser.urlencoded({extended: false, limit: '50mb'}));
const fs = require("fs")
const path = require('path');
const moment = require('moment');
//使用db_nenghuan
const visual_db = require("../../db/visual_db.js");
// let filepath = 'D:/GF/imanage_qrk/'

//视觉识别系统 查询历史数据------------------------------------
router.post("/route_visual/visual_searchKNR", (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    var para = req.body
        // let sqlParam = [para.KNR]
    visual_db.visual_searchKNR("visual", para, res);

    // db.s7path("sql_s7", para, res);
})

//视觉s识别系统 查询实时KNR号和结果------------------------------------
router.post("/route_visual/getTableData", (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    var para = req.body.db
        // let sqlParam = [para.KNR]
    visual_db.getTableData("visual", para, res);

    // db.s7path("sql_s7", para, res);
})

//视觉s识别系统 查询实时KNR号和结果------------------------------------
router.post("/route_visual/getTableData4050", (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    var para = req.body.db
        // let sqlParam = [para.KNR]
    visual_db.getTableData4050("visual", para, res);

    // db.s7path("sql_s7", para, res);
})
//CP8视觉
router.post("/route_visual/getKNRData", (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    var para = req.body.db
        // let sqlParam = [para.KNR]
    visual_db.getKNRData("visual", para, res);

})
//cp8工位屏
router.post("/route_visual/getvoicdate", (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    var para = req.body.db
        // let sqlParam = [para.KNR]
    visual_db.getvoicdate("visual", para, res);


   
})

//cp8工位屏2
router.post("/route_visual/getvoicdate2", (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    var para = req.body.db
        // let sqlParam = [para.KNR]
    visual_db.getvoicdate2("visual", para, res);


   
})



//QRK视觉管理系统 manage_qrk页面   读取图片文件  
router.post("/route_visual/QRKreadImg", (req, res) => {
        let imgpath = req.body.path
        fs.stat(imgpath, (err, data) => {
            if (err) {
                res.send('300')
                return;
            }
            if (data.isFile()) {
                fs.readFile(imgpath, (err, data) => {
                    if (err) {
                        // console .log('读取错误', err);
                        res.send('300')
                        return;
                    }
                    // console .log(data, '读取文件成功');
                    res.send(data)
                        // res.send({
                        //     data: data,
                        // })

                    // console .log(data.toString()); //把Buffer转化成string类型
                })
            } else {
                res.send('300')
            }
        })

    }),

    //视觉驾驶舱获取识别点列表------------------------------------
    router.post("/route_visual/getpassrale", (req, res) => {
        res.setHeader('Access-Control-Allow-Origin', '*');
        var para = req.body.date
        // let sqlParam = [para.KNR]
        visual_db.getpassrale("visual",para, res);

        // db.s7path("sql_s7", para, res);
    })

//获取日周月年时间段的合格率------------------------------------
router.post("/route_visual/gethegelv", (req, res) => {
        res.setHeader('Access-Control-Allow-Origin', '*');
        // var para = req.body.db
        // let sqlParam = [para.KNR]
        visual_db.gethegelv("visual", res);

        // db.s7path("sql_s7", para, res);
    })
    //下载数据------------------------------------
router.post("/route_visual/downloadExcel", (req, res) => {
        res.setHeader('Access-Control-Allow-Origin', '*');
        var para = req.body
            // let sqlParam = [para.KNR]
        visual_db.downloadExcel("visual", para, res);

        // db.s7path("sql_s7", para, res);
    })
    //QRK4050视觉   获取岗位信息  
router.post("/getworkpost", (req, res) => {
        res.setHeader('Access-Control-Allow-Origin', '*');
        var sqlParam = [req.body.postName, req.body.KNR, req.body.station]

        db_qrk.getworkpost("qrk", sqlParam, res);

    }),
router.post("/route_visual/startCollect", (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    visual_db.startCollect("D:","paige_main","paige_main", res);
    // visual_db.startCollect("E:","exe","andonLog", res);

}),
router.post("/route_visual/ifrunning", (req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    visual_db.ifrunning("paige_main", res);
    // visual_db.ifrunning("andonLog", res);

}),
    module.exports = router